/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Events;

namespace Core.Model.Data.Variables
{
    public class IntegerVariable : Variable
    {

        #region  Exchanged Attributes
        private int originalValue;

        public int OriginalValue
        {
            get { return originalValue; }
            set { originalValue = value; }
        }
        #endregion

        #region  Internal Attributes
        private int value;

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();

            value = originalValue;
        }
        #endregion

        #region  Actions
        
        public void SetVariable(int value)
        {
            this.value = value;
        }

        public void Add ( int value )
        {
            this.value += value;
        }

        public void Subtract ( int value )
        {
            this.value -= value;
        }

        public void Multiply ( int value )
        {
            this.value *= value;
        }

        public void Divide ( int value )
        {
            this.value /= value;
        }

        public void Modulo ( int value )
        {
            this.value %= value;
        }

        public void TestVariable ( ComparasonTests op, int comparisonValue )
        {
            bool res = false;

            switch ( op )
            {
                case ComparasonTests.Equal:
                    res = ( value == comparisonValue );
                    break;

                case ComparasonTests.NotEqual:
                    res = ( value != comparisonValue );
                    break;

                case ComparasonTests.LessThan:
                    res = ( value < comparisonValue );
                    break;

                case ComparasonTests.LessThanOrEqualTo:
                    res = ( value <= comparisonValue );
                    break;

                case ComparasonTests.GreaterThan:
                    res = ( value > comparisonValue );
                    break;

                case ComparasonTests.GreaterThanOrEqualTo:
                    res = ( value >= comparisonValue );
                    break;

                default:
                    base.TestVariable( op, comparisonValue );
                    return;
            }

            Engine.MHEGEngine.Instance.Handle( new TestEvent( this, res ) );
        }

        public override bool Equals ( Object o )
        {
            if ( this == o ) return true;
            if ( !( o is IntegerVariable ) ) return false;

            return ( value == (( IntegerVariable ) o).Value);

        }

        public override int GetHashCode ()
        {
            return value;
        }

        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public override object Data
        {
            get { return this.value; }
            set { this.value = (int)value; }
        }

        public override string ToString ()
        {
            return base.ToString() +":"+Value.ToString();
        }
        #endregion
    }
}
